﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Outcoder. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2011-08-21 16:16:19Z</CreationDate>
</File>
*/
#endregion

using System;

namespace Outcoder
{
	/// <summary>
	/// Extension methods for the <c>string</c> class.
	/// </summary>
	public static partial class StringExtensions
	{
		/// <summary>
		/// Determines whether the specified text contains the specified substring.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="substring">The substring.</param>
		/// <returns>
		/// 	<c>true</c> if the specified text contains the specified substring; 
		/// otherwise, <c>false</c>.
		/// </returns>
		/// <exception cref="ArgumentNullException">
		/// Occurs if either parameter is <c>null</c>.</exception>
		public static bool ContainsIgnoreCase(this string text, string substring)
		{
			ArgumentValidator.AssertNotNull(text, "text");
			ArgumentValidator.AssertNotNull(substring, "substring");

			return text.IndexOf(substring, StringComparison.OrdinalIgnoreCase) > -1;
		}

		/// <summary>
		/// Converts the specified string to a byte array using its raw character values.
		/// This is the companion method of <see cref="ConvertToUnencodedString"/>.
		/// </summary>
		/// <param name="text">The string value. Cannot be null.</param>
		/// <returns>The bytes of the characters in the string.</returns>
		public static byte[] ConvertToUnencodedBytes(this string text)
		{
			byte[] bytes = new byte[text.Length * sizeof(char)];
			Buffer.BlockCopy(text.ToCharArray(), 0, bytes, 0, bytes.Length);
			return bytes;
		}

		/// <summary>
		/// Converts the specified byte array to a string.
		/// This is the companion method of <see cref="ConvertToUnencodedBytes"/>.
		/// Do not use this if you wish to store the value in an XML file. 
		/// It may result in high-end character values that are incompatible with XML. 
		/// Instead use <see cref="ConvertToUtf8String"/>.
		/// </summary>
		/// <param name="bytes"></param>
		/// <returns>The string containing character made up of the byte array values.</returns>
		public static string ConvertToUnencodedString(this byte[] bytes)
		{
			char[] characters = new char[bytes.Length / sizeof(char)];
			Buffer.BlockCopy(bytes, 0, characters, 0, bytes.Length);
			return new string(characters);
		}

		/// <summary>
		/// Converts the specified string to a byte array using UTF8 character encoding.
		/// This is the companion method of <see cref="ConvertToUtf8String"/>.
		/// </summary>
		/// <param name="text">The string value. Cannot be null.</param>
		/// <returns>The bytes of the characters in the string.</returns>
		public static byte[] ConvertUtf8StringToBytes(this string text)
		{
			byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
			return bytes;
		}

		/// <summary>
		/// Converts a byte array to a UTF8 encoded string.
		/// This is the companion method of <see cref="ConvertUtf8StringToBytes"/>.
		/// </summary>
		/// <param name="bytes">The bytes containing UTF8 encoded character values.</param>
		/// <returns>A UTF8 encoded string.</returns>
		public static string ConvertToUtf8String(this byte[] bytes)
		{
			string result = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
			return result;
		}

		/// <summary>
		/// Converts the specified string to a byte array using UTF16 character encoding.
		/// This is the companion method of <see cref="ConvertToUtf16String"/>.
		/// </summary>
		/// <param name="text">The string value. Cannot be null.</param>
		/// <returns>The bytes of the characters in the string.</returns>
		public static byte[] ConvertUtf16StringToBytes(this string text)
		{
			byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text);
			return bytes;
		}

		/// <summary>
		/// Converts a byte array to a UTF16 encoded string.
		/// This is the companion method of <see cref="ConvertUtf16StringToBytes"/>.
		/// </summary>
		/// <param name="bytes">The bytes containing UTF16 encoded character values.</param>
		/// <returns>A UTF16 encoded string.</returns>
		public static string ConvertToUtf16String(this byte[] bytes)
		{
			string result = System.Text.Encoding.Unicode.GetString(bytes, 0, bytes.Length);
			return result;
		}
	}
}
